AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel( "models/roller_spikes.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetColor( 64, 64, 255, 255 )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.val2 = 0
	self.Active = 0
	LS_RegisterEnt(self.Entity)
	RD_AddResource(self.Entity, "energy", 50)
	RD_AddResource(self.Entity, "hydrogen", 0)
	RD_AddResource(self.Entity, "air", 0)
	RD_AddResource(self.Entity, "water", 50)

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(10)
	end
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 75

	local ent = ents.Create( "water_electrolyzer" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		end
	elseif (iname == "Overdrive") then
		if (value > 0) then
			if ( self.Active == 1 ) then
			end
			self.overdrive = 1
			self.overdrivefactor = value
		else
			if ( self.Active == 1 ) then
			end
			self.overdrive = 0
		end
	end
end


function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
end

function ENT:Use()
end

function ENT:Output()
	return 1
end

function ENT:Electrolyze_water()
	energy = RD_GetResourceAmount(self, "energy")
	water = RD_GetResourceAmount(self, "water")
	local inc = 24 + math.random(1, 15)

	if (energy >= math.Round(inc/2)) and (water >= inc) then
		
		RD_SupplyResource(self, "air", inc)
		RD_SupplyResource(self, "hydrogen", inc*2)
		RD_ConsumeResource(self, "energy", math.Round(inc/2))
		RD_ConsumeResource(self, "water", inc)
		
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", inc) end
	else
		self.Entity:EmitSound( "common/warning.wav" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
	end
		
	return
end

function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "water Electrolyzer (OFF)\nEnergy: " .. self.val1.."\n water: "..self.val2 )
	else
		self:SetOverlayText( "water Electrolyzer (ON)\nEnergy: " .. self.val1.."\n water: "..self.val2 )
	end
	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "energy")
	self.val2 = RD_GetResourceAmount(self, "water")
	if ( self.Active == 1 ) then
		self:Electrolyze_water()
	end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Active = 1
		else
			self.Active = 0
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
		end
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
